package smartcounters.androidclient;

import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import smartcounters.androidclient.WebServices.AuthenticationWebServiceClient;
import smartcounters.androidclient.WebServices.ConnectionError;
import smartcounters.androidclient.WebServices.InformationReaderWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.AuthenticatedUserCredentials;
import smartcounters.androidclient.model.Configuration;
import smartcounters.androidclient.model.UserCredentials;
import smartcounters.androidclient.model.UserInfo;
import smartcounters.androidclient.model.eOperationsEnum;

public class LoginActivity extends Activity implements OnClickListener, OnResultReceiver {
	private Button m_ButtonLogin;
	private TextView m_LoginMessageTextView;
	private EditText m_UsernameEditText;
	private EditText m_PasswordEditText;
	private EditText m_ServerIPAddress;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        
        m_ButtonLogin = (Button)findViewById(R.id.buttonLogin);
        m_LoginMessageTextView = (TextView)findViewById(R.id.loginMessageTextView);
        m_UsernameEditText = (EditText)findViewById(R.id.loginUsernameEditText);
        m_PasswordEditText = (EditText)findViewById(R.id.passwordEditText);
        m_ServerIPAddress = (EditText)findViewById(R.id.serverEditText);
        m_ServerIPAddress.setText("10.0.2.2");
        m_ButtonLogin.setOnClickListener(this);
    }

	public void onClick(View arg0) {
        try{
        	m_LoginMessageTextView.setText("Logging in...");
        	AuthenticationWebServiceClient client = new AuthenticationWebServiceClient(m_ServerIPAddress.getText().toString());
        	client.RegisterResultReceiver(this);
        	client.Authenticate(new UserCredentials(m_UsernameEditText.getText().toString(), m_PasswordEditText.getText().toString()));
        }
        catch (Exception ex){
        	ex.printStackTrace();
        }
	}

	public void ReceiveResult(Message i_Msg) {
		if (i_Msg.obj == null){
            m_LoginMessageTextView.setText("Wrong Username Or Password.");
        }
        else{
        	if (i_Msg.obj instanceof ConnectionError){
        		m_LoginMessageTextView.setText("Connection Error.");
        	}
        	else{
        		if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.Authenticate){
        			AuthenticatedUserCredentials authenticatedUserCredentials = (AuthenticatedUserCredentials) i_Msg.obj;
        			Configuration.getInstance().setServerAddress(m_ServerIPAddress.getText().toString());
        			Configuration.getInstance().setUserCredentials(authenticatedUserCredentials);
        			
        			InformationReaderWebServiceClient readerClient = new InformationReaderWebServiceClient(Configuration.getInstance().getServerAddress());
        			readerClient.RegisterResultReceiver(this);
        			readerClient.GetUserInformationByID(Configuration.getInstance().getUserCredentials(), Configuration.getInstance().getUserCredentials().getID());
        		}
        		else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetUserInformationByID){
        			UserInfo userInfo = (UserInfo)i_Msg.obj;
        			Configuration.getInstance().setLoggedInUser(userInfo);
        			
        			m_LoginMessageTextView.setText("Logged In.");
            		
            		Intent intent = new Intent();
            	    intent.setClass(this, MainActivity.class);
            	    startActivity(intent);
        		}
        	}
        }
	}
}
